{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "importing-libraries",
   "metadata": {},
   "source": [
    "## Importing Libraries\n",
    "\n",
    "First, we import the necessary libraries for data manipulation, downloading financial data, and plotting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "code-import-libraries",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from utils import plot_rolling_mean\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-import-libraries",
   "metadata": {},
   "source": [
    "Here is a breakdown of the libraries being used:\n",
    "- **pandas**: For data manipulation and analysis.\n",
    "- **plot_rolling_mean**: A custom function (from the `utils` module) that is used for plotting cumulative and rolling mean returns."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "creating-dataframe",
   "metadata": {},
   "source": [
    "## Loading S&P500 Futures Data\n",
    "\n",
    "We load the S&P500 futures data from a CSV file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "code-load-data",
   "metadata": {},
   "outputs": [],
   "source": [
    "prices = pd.read_csv(\"../data/sp500.csv\", index_col=0, parse_dates=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-load-data",
   "metadata": {},
   "source": [
    "We use `pd.read_csv()` to load the S&P500 futures data from `sp500.csv`. The `index_col=0` parameter sets the first column as the index, and `parse_dates=True` ensures that the index is interpreted as dates, which is essential for time series data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "computing-returns",
   "metadata": {},
   "source": [
    "## Computing Daily Returns\n",
    "\n",
    "Next, we calculate the daily percentage changes in the S&P500 futures prices to get the daily returns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "code-compute-returns",
   "metadata": {},
   "outputs": [],
   "source": [
    "returns = prices.pct_change()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-compute-returns",
   "metadata": {},
   "source": [
    "The `pct_change()` function computes the percentage change between consecutive rows, which gives us the daily returns of the S&P500 futures."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "calculating-cumulative-returns",
   "metadata": {},
   "source": [
    "## Calculating Cumulative Returns\n",
    "\n",
    "We calculate the cumulative returns over the period using the daily returns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "code-cumulative-returns",
   "metadata": {},
   "outputs": [],
   "source": [
    "cumulative_returns = (1 + returns).cumprod()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-cumulative-returns",
   "metadata": {},
   "source": [
    "By adding 1 to the daily returns and taking the cumulative product, we compute the cumulative returns. This gives us the total return over time, assuming the returns are compounded daily."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "calculating-rolling-mean",
   "metadata": {},
   "source": [
    "## Calculating Rolling Annualized Mean Returns\n",
    "\n",
    "We calculate the rolling mean returns over different time periods (1 year, 3 years, and 10 years). The rolling mean return provides a smoothed return by averaging over a rolling window of a specified size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "code-rolling-mean",
   "metadata": {},
   "outputs": [],
   "source": [
    "rolling_mean_1yr = returns.rolling(252).mean() * 252\n",
    "rolling_mean_3yrs = returns.rolling(756).mean() * 252\n",
    "rolling_mean_10yrs = returns.rolling(2520).mean() * 252"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-rolling-mean",
   "metadata": {},
   "source": [
    "Here is how we calculate the rolling mean returns:\n",
    "- **1-year rolling mean**: We use a window size of 252 trading days (which is approximately one year of trading) and annualize the result by multiplying by 252.\n",
    "- **3-year rolling mean**: We use a window size of 756 trading days (3 years of trading).\n",
    "- **10-year rolling mean**: We use a window size of 2520 trading days (10 years of trading).\n",
    "\n",
    "The rolling means are annualized by multiplying by 252 (the number of trading days in a year)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "plotting-results",
   "metadata": {},
   "source": [
    "## Plotting the Results\n",
    "\n",
    "Finally, we use a custom plotting function `plot_rolling_mean()` to plot the cumulative returns along with the rolling mean returns for the different time windows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "code-plotting",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 126040 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_rolling_mean(cumulative_returns, rolling_mean_1yr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c36a3f32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 114164 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_rolling_mean(cumulative_returns, rolling_mean_3yrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3dea5884",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 107724 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_rolling_mean(cumulative_returns, rolling_mean_10yrs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-plotting",
   "metadata": {},
   "source": [
    "The `plot_rolling_mean()` function is used to plot the cumulative returns and overlay the rolling mean returns for 1-year, 3-year, and 10-year periods. This allows us to visually compare the smoothed returns over different time horizons."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
